package de.muehle;


import java.awt.Color;
import java.util.Vector;

import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.geometry.*;

public class VentilShape extends Shape3D {
//  private static final float[] verts = {
//  // front face
//       1.0f, -1.0f,  .0f,                             1.0f,  1.0f,  .0f,
//      -1.0f,  1.0f,  .0f,                            -1.0f, -1.0f,  .0f,
//  // back face
//      -1.0f, -1.0f, .0f,                            -1.0f,  1.0f, .0f,
//       -5.0f,  1.0f, .0f,                             -5.0f, -1.0f, .0f,
//  // right face
//       -1.0f, 1.0f, 0.0f,                             1.0f,  1.0f, 0.0f,
//       1.0f,  5.0f,  0.0f,                             -1.0f, 5.0f,  0.0f,
//  // left face
//      1.0f, 1.0f,  0.0f,                              1.0f,  -1.0f,  0.0f,
//      5.0f,  -1.0f, 0.0f,                             5.0f, 1.0f, 0.0f,
//  // top face
//       1.0f,  -1.0f,  0.0f,                             -1.0f,  -1.0f, 0.0f,
//      -1.0f,  -5.0f, 0.0f,                            1.0f,  -5.0f,  0.0f
//  };
//
//private static final float[] colors = {
//  // front face (red)
//      1.0f, 0.0f, 0.0f,                            1.0f, 0.0f, 0.0f,
//      1.0f, 0.0f, 0.0f,                            1.0f, 0.0f, 0.0f,
//  // back face (green)
//      0.0f, 1.0f, 0.0f,                            0.0f, 1.0f, 0.0f,
//      0.0f, 1.0f, 0.0f,                            0.0f, 1.0f, 0.0f,
//  // right face (blue)
//      0.0f, 0.0f, 1.0f,                            0.0f, 0.0f, 1.0f,
//      0.0f, 0.0f, 1.0f,                            0.0f, 0.0f, 1.0f,
//  // left face (yellow)
//      1.0f, 1.0f, 0.0f,                            1.0f, 1.0f, 0.0f,
//      1.0f, 1.0f, 0.0f,                            1.0f, 1.0f, 0.0f,
//  // top face (magenta)
//      1.0f, 0.0f, 1.0f,                            1.0f, 0.0f, 1.0f,
//      1.0f, 0.0f, 1.0f,                            1.0f, 0.0f, 1.0f
//  };
	private Geometry geometry = null; //TODO
	private Appearance appearance = null;
	private float width;
	private float tief;
	private float breite;
	
	public VentilShape(int width, int height)
	{
		super();
		setGeometry(geometry);
	}
	
	public VentilShape(float width, float breite, float tiefe)
	{
		super();
		this.width = width;
		this.breite = breite;
		this.tief = tiefe;
		geometry = createGeometry();
		appearance = createAppearance();
		this.setGeometry(geometry);
		this.setAppearance(appearance);
	}

	private Appearance createAppearance() {
		Appearance appearance = new Appearance();
		Color3f color = new Color3f(Color.yellow);
		Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
		Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
		Texture texture = new Texture2D();
		TextureAttributes texAttr = new TextureAttributes();
		texAttr.setTextureMode(TextureAttributes.MODULATE);
		texture.setBoundaryModeS(Texture.WRAP);
		texture.setBoundaryModeT(Texture.WRAP);
		texture.setBoundaryColor(new Color4f(0.0f, 1.0f, 0.0f, 0.0f));
		Material mat = new Material(color, black, color, white, 70f);
		appearance.setTextureAttributes(texAttr);
		appearance.setMaterial(mat);
		appearance.setTexture(texture);
		return appearance;
	}

	private Geometry createGeometry() {
		LineArray pyramidGeometry = new LineArray(24*3,	LineArray.COORDINATES);//| QuadArray.COLOR_3);
		pyramidGeometry.setCoordinates(0, getVentilLine());
		return pyramidGeometry;
	}

	private Point3f[] getVentilLine() {
		Vector<Point3f> allPoints1 = getVectorPoly(width, breite, 0);
		Vector<Point3f> allPoints2 = getVectorPoly(width, breite, tief);
		Point3f[] tempf = new Point3f[3*24];
		for(int i = 0; i < 12; i++)
		{
			tempf[2*i] = allPoints1.get(i);
			tempf[2*i+1] = (Point3f) allPoints1.get((i+1)%12).clone();
		}
		for(int i = 0; i < 12; i++)
		{
			tempf[24+2*i] = allPoints1.get(i);
			tempf[24+2*i+1] = allPoints2.get(i);
		}
		for(int i = 0; i < 12; i++)
		{
			tempf[48+2*i] = allPoints2.get(i);
			tempf[48+2*i+1] = (Point3f) allPoints2.get((i+1)%12).clone();
		}
		return tempf;
	}

	private Vector<Point3f> getVectorPoly(float base, float breite, float tiefe) {
		Vector<Point3f> allPoints = new Vector<Point3f>();
		allPoints.add(new Point3f(-base/2, tiefe, -breite/2));//1
		allPoints.add(new Point3f(-breite/2, tiefe, -breite/2));//2
		allPoints.add(new Point3f(-breite/2, tiefe, -base/2));//3
		allPoints.add(new Point3f(breite/2, tiefe, -base/2));//4
		allPoints.add(new Point3f(breite/2, tiefe, -breite/2));//5
		allPoints.add(new Point3f(base/2, tiefe, -breite/2));//6
		allPoints.add(new Point3f(base/2, tiefe, breite/2));//7
		allPoints.add(new Point3f(breite/2, tiefe, breite/2));//8
		allPoints.add(new Point3f(breite/2, tiefe, base/2));//9
		allPoints.add(new Point3f(-breite/2, tiefe, base/2));//10
		allPoints.add(new Point3f(-breite/2, tiefe, breite/2));//11
		allPoints.add(new Point3f(-base/2, tiefe, breite/2));//12
		return allPoints;
	}
}